package org.com.java_chatroom.api;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Mapper;
import org.com.java_chatroom.model.Friend;
import org.com.java_chatroom.model.FriendMapper;
import org.com.java_chatroom.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FriendAPI {
    @Autowired
    private FriendMapper friendMapper ;
//    获取好友列表:
    @RequestMapping("/friendList")
    @ResponseBody
    public Object getFriendList(HttpServletRequest req){
        HttpSession session = req.getSession(false);
        if(session==null){
            System.out.println("[FriendAPI] getFriendList 获取session失败");
            return new ArrayList<Friend>();
        }
        User user = (User) session.getAttribute("user");
        if(user==null){
            System.out.println("[FriendAPI] getFriendList user不存在");
            return new ArrayList<Friend>();
        }
        List<Friend> friendList = friendMapper.getFriendList(user.getUserId());
        return friendList;
    }
}
